<%--
    File        : Categories
    Created     : 10.6.2014
    Author      : Václav Bukovský
    Description : Jsp page which serve all operation with categories and displays all categories in database with their
    name and mediums.
    Operation should be : create category, delete category, search category by name and update category.
--%>
<%@page contentType="text/html" pageEncoding="UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>

<html>
    <head>
        <title>Categories Library</title>
        <link rel="stylesheet" type="text/css" href="library.css">
    </head>
<body>
    <div style="text-align: center">
        <h1>Categories Library</h1>

        <form method="post" action="<c:url value="index"/>" name="back" style="margin-bottom: 0;">
            <input type="submit" value="Home">
        </form>
    </div>
    <br/>

    <!-- Create category -->
    <div class="left">
        <h3>Create new category</h3>
        <form action="<c:url value="/createCategory" />" name="newCategory" method="post">
            <table border="1">
                <tr>
                    <th>Name of new category:</th>
                    <td><input type="text" name="name" required maxlength="50"/></td>
                </tr>
            </table>
            <input type="Submit" value="Create" />
        </form>

        <!-- Create error -->
        <c:if test="${not empty createError}">
            <h4 style="color: red; background-color: black"><c:out value="${createError}"/></h4>
        </c:if>
        <c:if test="${not empty createMessage}">
            <h4 style="color: black; background-color: green;"><c:out value="${createMessage}"/></h4>
        </c:if>

        <div>
            <h3>All Categories</h3>

            <c:if test="${not empty listCategories}">
                <c:set var="msg" value="${listCategories}" scope="page" />
                <c:out value="${msg}" escapeXml="false" default="Nothing to show!"/>
            </c:if>
        </div>

    </div>

    <!-- Delete category -->
    <div class="right">
        <div>
            <form action="<c:url value="/deleteCategory"/>" name="deleteCategory" method="post">
                <table border="1">
                    <tr>
                        <th>Enter category name to delete: (category have to be empty!)</th>
                        <td><input type="text" name="delete" required/></td>
                    </tr>
                </table>
                <input type="submit" value="Delete">
            </form>

            <!-- Delete error -->
            <c:if test="${not empty deleteError}">
                <h4 style="color: red; background-color: black"><c:out value="${deleteError}"/></h4>
            </c:if>
            <c:if test="${not empty deletedCat}" >
                <h4 style="color: black; background-color: green"> Deletion successful. Category "${deletedCat}" removed.</h4>
            </c:if>
        </div>

        <hr/>

        <!-- Update category -> search pane -->
        <div>
            <form action="<c:url value="/updateCategory"/>" name="updateCategory" method="post">
                <table border="1">
                    <tr>
                        <th>Enter category name to update: </th>
                        <c:choose>
                            <c:when test="${not null and not empty searchedCategory}">
                                <td><input type="text" name="update" required value="${searchedCategory}"/></td>
                            </c:when>
                            <c:otherwise>
                                <td><input type="text" name="update" required/></td>
                            </c:otherwise>
                        </c:choose>
                    </tr>
                </table>
                <c:choose>
                    <c:when test="${not null and not empty searchedCategory}">
                        <input type="submit" value="Update" hidden />
                    </c:when>
                    <c:otherwise>
                        <input type="submit" value="Update"/>
                    </c:otherwise>
                </c:choose>
            </form>

            <!-- Update error -->
            <c:if test="${not empty updateError}">
                <h4 style="color: red; background-color: black"><c:out value="${updateError}"/></h4>
            </c:if>
        </div>

        <br/>

        <!-- Update medium -> update value -->
        <div>
            <c:if test="${not null and not empty searchedCategory}">
                <form action="<c:url value="/updateCategory"/>" name="updateCategory" method="post">
                    <table border="1">
                        <tr>
                            <th>Name of category:</th>
                            <td><input type="text" name="uName" required maxlength="50"/></td>
                        </tr>
                    </table>
                    <input type="Submit" value="Submit" />
                    <input type="Submit" value="Cancel" name="Cancel" />
                </form>

                <!-- Updatovane jmeno je jiz v databazi -->
                <c:if test="${not empty uError}">
                    <h4 style="color: red; background-color: black"><c:out value="${uError}"/></h4>
                </c:if>
            </c:if>
        </div>
    </div>
    </div>

</body>
</html>